package com.loongtech.bi.manager.realTime;

import com.loongtech.bi.action.UIHelper;
import com.loongtech.bi.entity.realTime.EntityCashAddByHourByTag;
import com.loongtech.bi.manager.realTime.OverviewManager.OverviewDetailData;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;
import com.loongtech.core.util.Time;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * 实时数据-小时新增付费金额manager
 * 
 * @author marin
 * @date 2015-09-15
 */
@Service
public class CashAddByHourByTagManager extends ManagerQueryCacheBase<EntityCashAddByHourByTag> {
	private static final long serialVersionUID = 1L;
	
	public List<EntityCashAddByHourByTag> getHourCashAddByDateByTag(Date date, String queryPlatform, String queryChannel, String queryServer, String queryCountry) {
		return findByQuery("from EntityCashAddByHourByTag where day>=:dayStart and day<:dayEnd and platformid=:platformid and channelid=:channelid and serverid=:serverid and tagvalue=:tagvalue order by hour ", "dayStart", new Date(Time.getBeginningOfDay(date.getTime())), "dayEnd", new Date(Time.getBeginningOfDay(date.getTime() + Time.DAY)), "platformid", queryPlatform, "channelid", queryChannel, "serverid", queryServer,  "tagvalue", queryCountry);
	}
	
	// 根据国家、区服更新小时付费金额数据
	public void updateHourCashAddByConditionsByTag(String updateType, Map<Integer, OverviewDetailData> map, Date lookDate, Date compareDate, Date lastWeekDate, String queryPlatform, String queryChannel, String queryServer, String queryCountry) {
		List<EntityCashAddByHourByTag> ooLookDate = getHourCashAddByDateByTag(lookDate, queryPlatform, queryChannel, queryServer, queryCountry);
		List<EntityCashAddByHourByTag> ooCompareDate = getHourCashAddByDateByTag(compareDate, queryPlatform, queryChannel, queryServer, queryCountry);
        List<EntityCashAddByHourByTag> ooLastWeekDate = getHourCashAddByDateByTag(lastWeekDate, queryPlatform, queryChannel, queryServer, queryCountry);
        if ("1".equals(updateType)) {  // 分时
            updateData(map, ooLookDate, ooCompareDate, ooLastWeekDate);
        } else if ("2".equals(updateType)) {  // 累加
            updateData1(map, ooLookDate, ooCompareDate, ooLastWeekDate);
        }
	}
	
	// 更新结果-分时
	private void updateData(Map<Integer, OverviewDetailData> map, List<EntityCashAddByHourByTag> ooLookDate, List<EntityCashAddByHourByTag> ooCompareDate, List<EntityCashAddByHourByTag> ooLastWeekDate) {
		for (EntityCashAddByHourByTag o : ooLookDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setPayLookValue(o.getCashadd() + odd.getPayLookValue());
			}
		}
		for (EntityCashAddByHourByTag o : ooCompareDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setPayCompareValue(o.getCashadd() + odd.getPayCompareValue());
			}
		}
        for (EntityCashAddByHourByTag o : ooLastWeekDate) {
            OverviewDetailData odd = map.get(o.getHour());
            if (odd != null) {
                odd.setPayLastWeekValue(o.getCashadd() + odd.getPayLastWeekValue());
            }
        }
		for (Entry<Integer, OverviewDetailData> entry : map.entrySet()) {
			OverviewDetailData t = entry.getValue();
			t.setPayLookValue(UIHelper.getYuan(t.getPayLookValue()));
			t.setPayCompareValue(UIHelper.getYuan(t.getPayCompareValue()));
            t.setPayLastWeekValue(UIHelper.getYuan(t.getPayLastWeekValue()));
		}
	}

    // 更新结果-累计
    private void updateData1(Map<Integer, OverviewDetailData> map, List<EntityCashAddByHourByTag> ooLookDate, List<EntityCashAddByHourByTag> ooCompareDate, List<EntityCashAddByHourByTag> ooLastWeekDate) {
    	for (EntityCashAddByHourByTag o : ooLookDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setPayLookValue(o.getCashadd() + odd.getPayLookValue());
			}
		}
		for (EntityCashAddByHourByTag o : ooCompareDate) {
			OverviewDetailData odd = map.get(o.getHour());
			if (odd != null) {
				odd.setPayCompareValue(o.getCashadd() + odd.getPayCompareValue());
			}
		}
        for (EntityCashAddByHourByTag o : ooLastWeekDate) {
            OverviewDetailData odd = map.get(o.getHour());
            if (odd != null) {
                odd.setPayLastWeekValue(o.getCashadd() + odd.getPayLastWeekValue());
            }
        }

        // 累计规则：三个日期中，与本日相同的那个的累计规则特殊，需要判断它的数据列表中最大的hour，这个hour之后的数据不累加且计为0
        String todayStr = Time.getDateStr(Time.currentDate());
        OverviewDetailData oneData = map.get(0);
        boolean isLookToday = todayStr.equals(oneData.getLookDateStr());
        int maxLookHour = (ooLookDate == null || ooLookDate.size() == 0) ? 0 : ooLookDate.get(ooLookDate.size() - 1).getHour();
        boolean isCompareToday = todayStr.equals(oneData.getCompareDateStr());
        int maxCompareHour = (ooCompareDate == null || ooCompareDate.size() == 0) ? 0 : ooCompareDate.get(ooCompareDate.size() - 1).getHour();
        boolean isLastWeekToday = todayStr.equals(oneData.getLastWeekDateStr());
        int maxLastWeekHour = (ooLastWeekDate == null || ooLastWeekDate.size() == 0) ? 0 : ooLastWeekDate.get(ooLastWeekDate.size() - 1).getHour();
		
		int sum1 = 0, sum2 = 0, sum3 = 0;
		// 由于传入的map子类是LinkedHashMap且根据hour顺序初始化，故不再寻找最大hour从0开始累计
		for (Entry<Integer, OverviewDetailData> entry : map.entrySet()) {
            int hour = entry.getKey();

			OverviewDetailData t = entry.getValue();
			sum1 += t.getPayLookValue();
            if (!isLookToday || hour <= maxLookHour) {
                t.setPayLookValue(sum1);
            }
			sum2 += t.getPayCompareValue();
            if (!isCompareToday || hour <= maxCompareHour) {
                t.setPayCompareValue(sum2);
            }
			sum3 += t.getPayLastWeekValue();
            if (!isLastWeekToday || hour <= maxLastWeekHour) {
                t.setPayLastWeekValue(sum3);
            }

			if (t.getPayLookValue() > 0) {
				t.setPayLookValue(UIHelper.getYuan(t.getPayLookValue()));
			}
			if (t.getPayCompareValue() > 0) {
				t.setPayCompareValue(UIHelper.getYuan(t.getPayCompareValue()));
			}
			if (t.getPayLastWeekValue() > 0) {
				t.setPayLastWeekValue(UIHelper.getYuan(t.getPayLastWeekValue()));
			}
		}
	}
}
